import { ref, watch, reactive, getCurrentInstance, computed } from 'vue'
import { Search, Aim, Download, Delete } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import request from '@/utils/request'
import { publicApi } from '../../../../api/public'
import entComp from './component/entComp'
import staticCell from './component/staticCell'
import buttonCell from './component/buttonCell'
import $button from '@/button'
export const tableItem = reactive({
    select: true,
    index: !true,
    // tree: true,
    expandAll: true,
    align: 'center',
    loading:false,
    column: [
        {
            label: '姓名',
            prop: 'name',
            align: 'center',
        },
        {
            label: '身份证号',
            prop: 'idCard',
            align: 'center',
        },
        {
            label: '手机号',
            prop: 'phone',
            align: 'center',
        },
        {
            label: '批次号',
            prop: 'batchNum',
            align: 'center',
        },
        {
            label: '考试科目',
            prop: 'courseName',
            align: 'center',
        },
        {
            label: '所属企业',
            prop: 'entName',
            align: 'center',
            width: 200
            // component: entComp,
        },
        {
            label: '最近练习时间',
            prop: 'lastPracticeTime',
            align: 'center',
        },
        {
            label: '账号期限',
            prop: 'accountPeriod',
            align: 'center',
            width: 170
        },
        {
            label: '题库类型',
            prop: 'courseBankTypeName',
            align: 'center',
        },
        {
            label: '状态',
            component:staticCell,
            align: 'center',
            width:'auto'
        },
        {
            label: '备注',
            prop: 'remark',
            align: 'left',
            tooltip: true
        },
        {
            label: '操作',
            prop: 'action',
            align: 'center',
            width: 120,
            component:buttonCell,
        }
    ],
    pageInfo: {
        request: {
            statusList:[1,2,3],
            // 'entName': '',
            'name': '',
            'idCard': '',
            'batchNum': '',
            'courseName': '',
            'courseId': '',
            'courseBankTypeId': '',
            'courseBankTypeName': '',
            'subjectArray': [],
            'lineDateStart': computed(() => {
                if (tableItem.pageInfo.request.createDateStart) {
                    return tableItem.pageInfo.request.createDateStart + "T00:00:00"
                } else {
                    return ""
                }
            }),
            'lineDateEnd': computed(() => {
                if (tableItem.pageInfo.request.createDateEnd) {
                    return tableItem.pageInfo.request.createDateEnd + "T23:59:59"
                } else {
                    return ""
                }
            }),
            'pageDomain': {
                'pageNum': '1',//分页页面数
                'pageSize': '10',//分页页面数据大小
                'orderByColumn': '',//搜索参数
                'isAsc': '',//搜索参数
                'reasonable': ''//搜索参数
            }
        },
        // url: '/student/page',//分页路由，不传的话需要传入自定义的分页函数paging 传入时定义的分页函数paging会失效
        // method: 'POST',//分页请求方法，不传默认为POST
        // 分页函数paging
        paging: function () {
            tableItem.loading=true
            studentPagingApi(tableItem.pageInfo.request).then((res) => {
                if (res.rows) {
                    tableItem.tableData.data = res.rows || [];
                    tableItem.tableData.total = res.total || 0
                }
                if (res.data) {
                    tableItem.tableData.data = res.data || [];
                    tableItem.tableData.total = res.total || 0
                }
                tableItem.tableData.data.forEach( (row) => {
                    if (row.startDate && row.lineDate) {
                        row.accountPeriod = row.startDate + "至" + row.lineDate
                    } else {
                        row.accountPeriod = ''
                    }
                })
                tableItem.loading=false
            }).catch(()=>{ tableItem.loading=false});
        }
    },
    // 分页数据 自定义分页函数paging时需要把值赋给tableData
    tableData: {
        total: '',//分页总计
        // 分页数据
        rows: []
    },
    // 操作栏按钮
    btnArray: [
        {
            label: '修改',
            type: 'primary',
            plain: true,//镂空按钮  优先文字按钮
            text: true,//文字按钮
            hasPermi: [$button.studentstudentedit|| null],
            customParameters: 'edit',//自定义函数参数
            show: true,//是否显示
            // 按钮方法
            handle: function (row) {
            }
        },
        {
            label: '删除',
            type: 'danger',
            hasPermi: [$button.studentstudentremove|| null],
            plain: true,//镂空按钮  优先文字按钮
            text: true,
            customParameters: 'edit',//自定义函数参数
            show: true,//是否显示
            // 按钮方法
            handle: function (row) {
            }
        },
    ],
    moreBtnArrayText: '',
    moreBtnArray: [],
    // 选择框触发
    selectChange: function () {
    },
}
)
// const selectInit = reactive(['institution', 'association'])
export const operationItem = reactive({
    defaultSearch: false,
    defaultRefresh: false,
    customTableHeader: true,
    SearchItem: [
        {
            code: 'name',
            name: '姓名',
            type: 'input',
        },
        {
            code: 'idCard',
            name: '身份证号',
            type: 'input',
        },
        {
            code: 'batchNum',
            name: '批次号',
            type: 'input',
        },
        {
            code: 'subjectArray',
            name: '考试科目',
            type: 'cascader',
            options: []
        },
        // {
        //     code: 'entName',
        //     name: '所属企业',
        //     type: 'select',
        //     options: []
        // },
        {
            closeSearch: false,
            code: 'courseBankTypeName',
            name: '题库类型',
            type: 'select',
            options: []
        },
    ],
    menuArray: [
        {
            show: true,
            type: 'primary',
            text: '查询',
            icon: Search,

            handle: () => {
            }
        },
        {
            show: true,
            type: 'primary',
            text: '导入',
            hasPermi: [$button.studentstudentimport || null, $button.studentstudentdownexcl || null],
            icon: Download,
            handle: () => {
            }
        },
        {
            show: true,
            type: 'primary',
            text: '指定企业',
            icon: Aim,
            hasPermi: [$button.studentstudentsetEnt|| null],
            handle: () => {
            }
        },
        {
            show: true,
            type: 'danger',
            text: '批量删除',
            hasPermi: [$button.studentstudentremove|| null],
            icon: Delete,
            handle: () => {
            }
        },
    ]
})

// 学员分页
export function studentPagingApi(dataRequest) {
    return request({
        url: '/student/page',
        method: 'POST',
        data: dataRequest
    })
}
// 学员详情
export function studentDetailApi(dataRequest) {
    return request({
        url: '/student/detail',
        method: 'POST',
        data: dataRequest
    })
}
//科目列表
export function courseListApi(dataRequest) {
    return request({
        url: "/course/list",
        method: "POST",
        data: dataRequest,
    });
}
//题库类型列表
export function bankTypeListApi(dataRequest) {
    return request({
        url: "/course/bank/type/enable/list",
        method: "POST",
        data: dataRequest,
    });
}

// 修改学员
export function studentEditApi(dataRequest) {
    return request({
        url: '/student',
        method: 'PUT',
        data: dataRequest
    })
}

// 删除学员
export function studentDelApi(dataRequest) {
    return request({
        url: '/student/batch/del',
        method: 'DELETE',
        data: dataRequest
    })
}

// 下载学员导入表格
export function studentDownloadApi() {
    return request({
        url: '/student/download/excl',
        method: 'POST',
    })
}

// 导入学员
export function studentImportApi(dataRequest) {
    return request({
        url: '/student/import/excl',
        method: 'POST',
        data: dataRequest
    })
}

// 指定企业
export function entSetApi(dataRequest) {
    return request({
        url: '/student/batch/setEnt',
        method: 'POST',
        data: dataRequest
    })
}
